﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using System.Transactions;
using System.Web;
using WebApp.Commons;
using WebApp.Data;

namespace WebApp.Services
{
    public class MaterialService
    {
        public List<MATERIAL_EXPAND_VIEW> GetMaterial(int index,
                                       int size,
                                       string sortColumn,
                                       string sortDirection,
                                       string keyword,
                                       ref int count)
        {
            using (var db = new PAVINGDataContext())
            {
                var direction = sortDirection.Equals("desc") ? " descending " : "";

                var mat = db.MATERIAL_EXPAND_VIEWs.Where(u => u.material_name.Contains(keyword) && u.material_type_id != 2)
                                              .OrderBy(sortColumn + direction)
                                              .Skip(size*(index - 1)).Take(size);

                count = mat.Count();

                return mat.ToList();
            }
        }
        
        public bool Update(tbl_material model)
        {
            try
            {
                using (var db = new PAVINGDataContext())
                {

                    tbl_material eModel;

                    if (model.material_id == 0)
                    {
                        model.material_create_by = PavingContext.CurrentUserId();
                        model.material_create_date = DateTime.Now;

                        db.tbl_materials.InsertOnSubmit(model);
                    }
                    else
                    {
                        eModel = db.tbl_materials.SingleOrDefault(s => s.material_id.Equals(model.material_id));

                        if (eModel != null)
                        {
                            eModel.material_name = model.material_name;
                            eModel.material_type_id = model.material_type_id;
                            eModel.tbl_mat_prices = model.tbl_mat_prices;

                            eModel.material_update_by = PavingContext.CurrentUserId(); ;
                            eModel.material_update_date = DateTime.Now;
                        }
                    }

                    db.SubmitChanges();

                    return true;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Delete(int[] ids)
        {
            using (var tran = new TransactionScope())
            {
                try
                {
                    using (var db = new PAVINGDataContext())
                    {

                        foreach (var id in ids)
                        {
                            var eModel = db.tbl_materials.SingleOrDefault(s => s.material_id.Equals(id));

                            if (eModel == null) continue;
                            db.tbl_materials.DeleteOnSubmit(eModel);
                            db.SubmitChanges();
                        }

                        tran.Complete();
                        return true;
                    }
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
        }

        public List<tbl_mat_price> GetMaterialPrice(int index,
                                                            int size,
                                                            string sortColumn,
                                                            string sortDirection,
                                                            int matid,
                                                            ref int count)
        {
            using (var db = new PAVINGDataContext())
            {
                var direction = sortDirection.Equals("desc") ? " descending " : "";

                var mat = db.tbl_mat_prices.Where(u => u.material_id == matid)
                                              .OrderBy(sortColumn + direction)
                                              .Skip(size * (index - 1)).Take(size);

                count = mat.Count();

                return mat.ToList();
            }
        }
    }
}